package com.redis.demo.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
* @program: SpringCloud
* @description: redis Key过期监听
* @author: zhang yis
* @create: 2020-03-24 14:14
*/
public class KeyExpiredListener extends KeyExpirationEventMessageListener {

   public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
       super(listenerContainer);
   }

   @Value("${redis.expiredKey}")
   private String expiredKey;


   @Override
   public void onMessage(Message message, byte[] pattern) {
       System.out.println("过期key:" + message.toString());
       //这个地方可以根据自己的业务来监听你锁需要的key
      if (message.toString().startsWith(expiredKey)){
         //业务调用.这里可以写自己得业务
          System.out.println("进行业务逻辑");
      }
   }
}
